﻿using SqlSugar;

namespace Digitalmes.Common.Extensions;

/// <summary>
/// 仓储扩展对象。
/// </summary>
public static class IRepositoryPagationExtensions
{
    /// <summary>
    /// 分页查询。
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="repo"></param>
    /// <param name="pageNumber">页号</param>
    /// <param name="pageSize">每页数量</param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    public static async Task<PagedList<T>> GetPagedListAsync<T>(this IRepository<T> repo,
         int pageNumber, int pageSize, CancellationToken cancellationToken = default)
         where T : class, IEntity, new()
    {
        return await repo.AsQueryable()
            .GetPagedListAsync(pageNumber, pageSize, cancellationToken);
    }

    /// <summary>
    /// 分页查询。
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="repo"></param>
    /// <param name="where">筛选条件</param>
    /// <param name="pageNumber">页号</param>
    /// <param name="pageSize">每页数量</param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    public static async Task<PagedList<T>> GetPagedListAsync<T>(this IRepository<T> repo, Expression<Func<T, bool>> where,
         int pageNumber, int pageSize, CancellationToken cancellationToken = default)
         where T : class, IEntity, new()
    {
        return await repo.AsQueryable()
            .Where(where)
            .GetPagedListAsync(pageNumber, pageSize, cancellationToken);
    }

    /// <summary>
    /// 可排序的分页查询。
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="repo"></param>
    /// <param name="pageNumber">页号</param>
    /// <param name="pageSize">每页数量</param>
    /// <param name="order">排序</param>
    /// <param name="isDesc">是否按逆序查询</param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    public static async Task<PagedList<T>> GetPagedListAsync<T>(this IRepository<T> repo,
        int pageNumber, int pageSize,
        Expression<Func<T, object>> order, bool isDesc = true, CancellationToken cancellationToken = default)
        where T : class, IEntity, new()
    {
        return await repo.AsQueryable()
            .OrderBy(order, isDesc ? OrderByType.Desc : OrderByType.Asc)
            .GetPagedListAsync(pageNumber, pageSize, cancellationToken);
    }

    /// <summary>
    /// 可排序的分页查询。
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="repo"></param>
    /// <param name="where">筛选条件</param>
    /// <param name="pageNumber">页号</param>
    /// <param name="pageSize">每页数量</param>
    /// <param name="order">排序</param>
    /// <param name="isDesc">是否按逆序查询</param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    public static async Task<PagedList<T>> GetPagedListAsync<T>(this IRepository<T> repo, Expression<Func<T, bool>>? where,
        int pageNumber, int pageSize,
        Expression<Func<T, object>> order, bool isDesc = true, CancellationToken cancellationToken = default)
        where T : class, IEntity, new()
    {
        return await repo.AsQueryable()
            .WhereIF(where != null, where)
            .OrderBy(order, isDesc ? OrderByType.Desc : OrderByType.Asc)
            .GetPagedListAsync(pageNumber, pageSize, cancellationToken);
    }
}
